<template>
    <div class="xunnang">
        <div class="xunnangTop">
            Kiểm toán rủi ro
        </div>
        <div class="xunnangContent">
            <t-loading theme="dots" size="4rem" />
            <p class="precent">{{ precentNum }}%</p>
            <p class="oneTip">Đang xết duyệ hồ sơ của bạn</p>
            <p class="twoTip">Quá trình xem xét đang được đẩy nhanh, vui lòng kiên nhẫn chờ đợi</p>
            <p class="threeTip">Quá trình này sẽ nhanh chóng, xin đừng rời đi</p>

        </div>
    </div>
</template>

<script setup lang='ts'>
import api from '@/api';
import router from '@/router';
import { getItem } from '@/utils/token-utils';
import { Toast } from 'tdesign-mobile-vue';
import { onMounted, onUnmounted, ref } from 'vue';

let precentNum = ref(0)

let timer:any;
let riskResTimer:any;

const callRiskControlRules = async () => {
    try {
        let riskRes = (await api.callRiskControlRules({
            uuid: getItem('uuid'),
            token: getItem('token')
        })).data
        if (riskRes.code == 0) {
            riskResTimer = setInterval(async () => {
                try {
                    let riskResXunhuan = (await api.getRiskResult({
                        uuid: getItem('uuid'),
                        token: getItem('token')
                    })).data
                    if (riskResXunhuan.code == 0) {
                        router.replace({
                            path: '/confirmLoan',
                            query:{
                                applyId:riskResXunhuan.data.applyId
                            }
                        })
                        clearInterval(riskResTimer)
                    } else if (riskResXunhuan.code == 15 || riskResXunhuan.code == 9) {
                        router.replace({
                            path: '/home'
                        })
                    } else if (riskResXunhuan.code != 3) {
                        // Toast({
                        //     message: riskRes.message,
                        //     duration: 3 * 1000
                        // })
                        // router.replace({
                        //     path: '/home'
                        // })
                    }

                } catch (error) {
                    console.log(error);

                }

            }, 3000)
        } else {
            Toast({
                message: riskRes.message,
                duration: 3 * 1000
            })
            router.replace({
                path: '/home'
            })
        }

    } catch (error) {
        console.log(error);

    }
}
onMounted(() => {
    timer = setInterval(() => {
        precentNum.value++;
        if (precentNum.value == 97) {
            clearInterval(timer)
        }
    }, 300)


    callRiskControlRules()

})

onUnmounted(() => {
    clearInterval(timer);
    clearInterval(riskResTimer);

})
</script>

<style lang="less" scoped>
.xunnang {
    height: 100%;
    background-color: #F7F7FA;

    .xunnangTop {
        min-height: 2.3rem;
        display: flex;
        justify-content: center;
        align-items: center;

        /* font-family: SF Pro, SF Pro; */
        font-weight: 600;
        font-size: 18px;
        color: #292B33;
        line-height: 26px;
        text-align: left;
        font-style: normal;
        text-transform: none;
    }

    .xunnangContent {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 0 .8rem;
        margin-top: 4.8rem;

        .precent {
            font-family: DIN Alternate, DIN Alternate;
            font-weight: bold;
            font-size: 1.8rem;
            color: #292B33;
            line-height: 2.4rem;
            text-align: center;
            font-style: normal;
            text-transform: none;
        }

        .oneTip {
            font-family: SF Pro, SF Pro;
            font-weight: bold;
            font-size: .7rem;
            color: #000000;
            line-height: 1.1rem;
            text-align: center;
            font-style: normal;
            text-transform: none;
            margin: .8rem 0 .3rem;
        }

        .twoTip {
            font-family: SF Pro, SF Pro;
            font-weight: 600;
            font-size: .8rem;
            color: #292B33;
            line-height: 1.2rem;
            text-align: center;
            font-style: normal;
            text-transform: none;
            margin-bottom: .6rem;
        }

        .threeTip {
            font-family: SF Pro, SF Pro;
            font-weight: 400;
            font-size: .7rem;
            color: #626673;
            line-height: 1.1rem;
            text-align: center;
            font-style: normal;
            text-transform: none;
        }
    }
}
</style>